// BESEthnicity
log using BESEthnicity.log, replace
use "C:\Users\sbstjp\OneDrive - Cardiff University\BES2019_W26_v26.0.dta" // Fieldhouse, E., J. Green, G. Evans, J. Mellon, C. Prosser, J. Bailey, J. Griffiths, S. Perrett. (2023) British Election Study Internet Panel Waves 1-26. DOI: 10.5255/UKDA-SN-8202-2// Accessed on March 11 2025.

// Social justice scale
* Clean "don't know" responses 
foreach var in cwTrans cwAuthors cwLanguage cwTraining {
    replace `var' = . if `var' == 9999
}

*Reverse variable so social justice coded high
foreach var in cwLanguage cwTraining {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' + 1 - `var'
}

*Standardize items in the scale from 1-2 - this avoids 0, for reasons outlined in next step
foreach var in cwTrans cwAuthors rcwLanguage rcwTraining {
    summarize `var'
    gen s`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}

* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values and means a 0-1 standardization scale isn't feasible as missing values would overlap with the scale
foreach var in scwTrans scwAuthors srcwLanguage srcwTraining {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_scoreSJV = 0
gen count_nonzeroSJV = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in scwTrans scwAuthors srcwLanguage srcwTraining {
    replace total_scoreSJV = total_scoreSJV + `var'
    replace count_nonzeroSJV = count_nonzeroSJV + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen SocJusValues = .
replace SocJusValues = total_scoreSJV / count_nonzeroSJV if count_nonzeroSJV > 0

// Demographics
* Delete missing values
replace p_gross_household = . if inlist(p_gross_household, 16, 17) 
replace p_ethnicity=. if p_ethnicity==16 
replace leftRight=. if leftRight==9999 

* Generate dummy variables
gen Graduate=.
replace Graduate=0 if inrange(p_edlevelUni, 0, 3) 
replace Graduate=1 if inlist(p_edlevelUni, 4, 5)

gen White=.
replace White=1 if inlist(p_ethnicity, 1, 2)
replace White=0 if inrange(p_ethnicity, 3, 15)

gen Black=.
replace Black=1 if inrange(p_ethnicity, 11, 13)
replace Black=0 if inrange(p_ethnicity, 1, 10)
replace Black=0 if inlist(p_ethnicity, 14, 15)

gen Multiracial=.
replace Multiracial=1 if inrange(p_ethnicity, 3, 6)
replace Multiracial=0 if inlist(p_ethnicity, 1, 2)
replace Multiracial=0 if inrange(p_ethnicity, 7, 15)

gen SouthAsian=.
replace SouthAsian=1 if inrange(p_ethnicity, 7, 10)
replace SouthAsian=0 if inrange(p_ethnicity, 1, 6)
replace SouthAsian=0 if inrange(p_ethnicity, 11, 15)

gen EastAsian=.
replace EastAsian=1 if p_ethnicity==14
replace EastAsian=0 if inrange(p_ethnicity, 1, 13)
replace EastAsian=0 if p_ethnicity==15

// Standardize 
egen Age = std(age)
egen Income = std(p_gross_household)
rename gender FemaleGender

// Regressions and correlations
regress SocJusValues Age FemaleGender Graduate Income White [pweight=wt], robust 
eststo
regress SocJusValues Age FemaleGender Graduate Income Black [pweight=wt], robust 
eststo
regress SocJusValues Age FemaleGender Graduate Income Multiracial [pweight=wt], robust
eststo
regress SocJusValues Age FemaleGender Graduate Income SouthAsian [pweight=wt], robust 
eststo
regress SocJusValues Age FemaleGender Graduate Income EastAsian [pweight=wt], robust 
eststo
esttab 

pwcorr SocJusValues White Black Multiracial SouthAsian EastAsian [aweight=wt], sig

log close
